var events =  {
		getEventsList : function() {
			var  crid = sessionStorage.getItem("crid");
			$.ajax({
				type : "GET",
				url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=getevents,crid=" + crid +"&address=http://103.37.166.4:7777/StateGrid/cra.do",
				scriptCharset: 'utf-8', 
				success : function(param) {
					param = JSON.parse(param);
					events.createEventsList(param.data);
				},
				error: function(){
				}
				
			});
		},
		createEventsList : function(param){
			var str = "";
			var j=0;
			$("#events_list").html("");
			for(var i=0;i<param.length;i++){
				j++;
				if(param[i].alive == "1"){
					events.currentEvent = param[i];
					str += '<li style="background: #fff9e8;" ><a href="javascript:void(0)">'+j+"、"+param[i].ceventName+"</a></li>";
					events.openVideo(param[i].materInfo.materFileName);
					messages.getMessages(param[i].ceventId,param[i].ownerClassRoomId);
					if($("#responsive").hasClass('active')) {
						questions.getQuestions();
					}
					$("#ceventHappenTime").html("发生："+param[i].ceventHappenTime);
					window.clearInterval(timer);
					createTime(param[i].ceventHappenTime);
				}else{
					str += '<li><a href="javascript:void(0)" onclick="events.confirmBegin(\''+param[i].ceventId+'\',\''+param[i].ownerClassRoomId+'\')">'+j+'、'+param[i].ceventName+'</a></li>';
				}
				
			}
			$(str).appendTo("#events_list");
		},
		openVideo : function(video_addr){
			$("#videoShow").addClass("active");
			$(".videoShow").css("visibility", "visible");
			$("#event_video").attr("src","img/pian.mp4");
			$("#videoName").html("视频："+events.currentEvent.ceventName);
//			$("#event_video").attr("src","flexUpDownLoad03/upload/"+video_addr);
		},
		beginEvent : function(){
			var eventid = events.oper_eventid;
			var crid = events.oper_crid;
			events.oper_crid = "";
			events.oper_eventid = "";
			$.ajax({
				type : "GET",
				url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=beginevent,eventid=" + eventid +",crid=" + crid +",alive=1&address=http://103.37.166.4:7777/StateGrid/cra.do",
				scriptCharset: 'utf-8', 
				success : function(param) {
					events.getEventsList();
				},
				error: function(){
				}
				
			});
		},
		endEvent : function(){
			var eventid = events.currentEvent.ceventId;
			var crid = events.currentEvent.ownerClassRoomId;
			$.ajax({
				type : "GET",
				url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=endevent,eventid=" + eventid +",crid=" + crid +",alive=0&address=http://103.37.166.4:7777/StateGrid/cra.do",
				scriptCharset: 'utf-8', 
				success : function(param) {
					$(".pop01").hide();
					$("body").removeClass("overHiden");
					events.beginEvent();
				},
				error: function(){
				}
				
			});
		},
		currentEvent : "",
		oper_eventid:"",
		oper_crid:"",
		confirmBegin : function(eventid,crid){
			events.oper_eventid = eventid;
			sessionStorage.setItem("eventid", eventid)
			events.oper_crid = crid;
			$("#message1").html("开启该事件？");
			$("#messages_btn").hide();
			$("#events_btn").show();
			$(".pop01").show();
			$("body").addClass("overHiden");
		},
		openEventsTips : function(){
			if(events.currentEvent == "" || events.currentEvent == null){
				$("#message2").html("请打开一个事件！");
				$(".pop02").show();
				$("body").addClass("overHiden");
				return false;
			}
			return true;
		}
	}

var messages = {
		getMessages : function(eventid,crid){
			if(events.openEventsTips()){
				$("#newShow").addClass("active");
				$(".newShow").css("visibility", "visible");
				$.ajax({
					type : "GET",
					url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=getmessages,eventid=" + eventid +",crid=" + crid +",sendtype=10&address=http://103.37.166.4:7777/StateGrid/crma.do",
					scriptCharset: 'utf-8', 
					success : function(param) {
						param = JSON.parse(param).data;
						messages.list = param;
						messages.createMessage(param);
					},
					error: function(){
					}
					
				});
			}
		},
		createMessage : function (messages){
			$("#messageList").html("");
			var j = 0;
			var str = "<tr><th>序号</th><th>消息编号</th><th>接收者</th><th>消息内容</th><th>发送时间</th><th>查看详情</th><th>发送状态</th></tr>";
			for(var i in messages){
				j++;
				var param = JSON.stringify(messages[i]).replace(new RegExp('"','gm'),'|');
				str += '<tr><td>'+j+'</td><td>'+messages[i].ecdEventMessageId+'</td><td title="'+messages[i].messageReceiverName+'">'+messages[i].messageReceiverName.substring(0,10)+'</td><td title="'+messages[i].ecdEventMessageContent+'">'+messages[i].ecdEventMessageContent.substring(0,10)+'</td><td>'+messages[i].ecdEventMessageSendTime+'</td><td id="seeDetail"><span class="seeDetail" onclick="messages.seeDetail(\''+i+'\')"></span></td><td>'
				if(messages[i].ecdEventMessageIsSended == '0'){
					str += '<span class="stateNo" onclick="messages.sendJudge(\'0\',\''+messages[i].ecdEventMessageId+'\')"></span></td></tr>'
				}else if(messages[i].ecdEventMessageIsSended == '1'){
					str += '<span class="stateOk" onclick="messages.sendJudge(\'1\',\''+messages[i].ecdEventMessageId+'\')"></span></td></tr>'
				}
			}
			$(str).appendTo("#messageList");
		},
		seeDetail : function (i){
			$(".detailPop").show(500);
			$(this).addClass("overHiden");
			message = messages.list[i];
			$("#ceventName").html(events.currentEvent.ceventName);
			$("#ecdEventMessageId").html(message.ecdEventMessageId);
			if(message.ecdEventMessageSendTypeId == 10){
				$("#ecdEventMessageSendTypeId").html("网络通知");
			}else if(message.ecdEventMessageSendTypeId == 20){
				$("#ecdEventMessageSendTypeId").html("手机短信通知");
			}else if(message.ecdEventMessageSendTypeId == 30){
				$("#ecdEventMessageSendTypeId").html("电话通知");
			}
			
			$("#ecdEventMessageSender").html(message.ecdEventMessageSender);
			$("#ecdEventMessageSendTime").html(message.ecdEventMessageSendTime);
			if(message.ecdEventMessageIsSended == "1"){
				$("#ecdEventMessageIsSended").html("已发送");
			}else{
				$("#ecdEventMessageIsSended").html("未发送");
			}
			$("#ceventDescription").val(message.ecdEventMessageContent);
			console.info(messages.list[i]);
		},
		list : {},
		searchMessage : function (){
			var condition = $("#condition").val();
			var condition_str = $("#condition_str").val();
			if(condition_str == ""){
				messages.createMessage(messages.list);
			}
			if(condition == "0") return;
			var new_list = messages.mapping(condition,condition_str);
			messages.createMessage(new_list);
		},
		mapping : function (condition,condition_str){
			var list = [];
			j = 0;
				if(condition == "0"){
					return;
				}else if(condition == "1"){
					for(var i in messages.list){
						if(messages.list[i].ecdEventMessageId.indexOf(condition_str)>-1){
							list[j] = messages.list[i];
							j++;
						}
					}
				}else if(condition == "2"){
					for(var i in messages.list){
						if(messages.list[i].messageReceiverName.indexOf(condition_str)>-1){
							list[j] = messages.list[i];
							j++;
						}
					}
				}else if(condition == "3"){
					for(var i in messages.list){
						if(messages.list[i].ecdEventMessageContent.indexOf(condition_str)>-1){
							list[j] = messages.list[i];
							j++;
						}
					}
				}else if(condition == "4"){
					for(var i in messages.list){
						if(messages.list[i].ecdEventMessageSendTime.indexOf(condition_str)>-1){
							list[j] = messages.list[i];
							j++;
						}
					}
				}
				return list;
		},
		addMessage : function(t){
			var sendorder = $("#sendorder").val();
			var sendtypeid = $("#sendtypeid").val();
			var sendtime = format($("#sendtime").val());
			var text = $("#text").val();
			var eventid = events.currentEvent.ceventId;
			var recvpost = users.selectedUser.ecdrepId;
			var sender = $("#sender").val();
			if(isNull(sendorder)&&isNull(sendtypeid)&&isNull(sendtime)&&isNull(text)&&isNull(eventid)&&isNull(recvpost)&&isNull(sender)){
				$.ajax({
					type : "GET",
					url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=createmsg,eventid=" + eventid +",sender=" + sender +",sendtime=" + sendtime +",recvpost=" + recvpost +",text=" + text +",sendtypeid=" + sendtypeid +",sendorder=" + sendorder +"&address=http://103.37.166.4:7777/StateGrid/crma.do",
					scriptCharset: 'utf-8', 
					success : function(param) {
						param = JSON.parse(param);
						if(param.stat == "1"){
							$(".temporaryAddPop").hide(500);
							$(t).removeClass("overHiden");
							messages.getMessages(events.currentEvent.ceventId,events.currentEvent.ownerClassRoomId);
							$("#message2").html("发送成功！");
							$(".pop02").show();
							$("body").addClass("overHiden");
						}
					},
					error: function(){
					}
					
				});
			}else if(sender == ""){
				$("#message2").html("请选择接收人！");
				$(".pop02").show();
				$("body").addClass("overHiden");
			}else{
				$("#message2").html("请填写完整！");
				$(".pop02").show();
				$("body").addClass("overHiden");
			}
		},
		send : function(){
			var crid = events.currentEvent.ownerClassRoomId;
			var eventid = events.currentEvent.ceventId;
			msgid = messages.sendMsgid;
			$.ajax({
				type : "GET",
				url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=updatesend,msgid=" + msgid +",crid=" + crid +",eventid=" + eventid + "&address=http://103.37.166.4:7777/StateGrid/crma.do",
				scriptCharset: 'utf-8', 
				success : function(param) {
					console.info(param);
					$(".pop01").hide();
					$("body").removeClass("overHiden");
					messages.getMessages(events.currentEvent.ceventId,events.currentEvent.ownerClassRoomId);
					$("#message2").html("发送成功！");
					$(".pop02").show();
					$("body").addClass("overHiden");
				},
				error: function(){
				}
				
			});
		},
		sendJudge : function(n,msgid){
			if(n == "0"){
				$("#message1").html("确认现在发送？");
			}else{
				$("#message1").html("消息条已经发送,确定要再次发送吗?");
			}
			messages.sendMsgid = msgid;
			$("#messages_btn").show();
			$("#events_btn").hide();
//			messages.send(msgid);
			$(".pop01").show();
			$("body").addClass("overHiden");
		},
		sendMsgid : ""
} 

var users = {
		getUsers : function(id){
			var usersCache = sessionStorage.getItem("usersCache" + events.currentEvent.ceventId);
			if(usersCache == null){
				$.ajax({
					type : "GET",
					url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=eventusers,eventid=" + events.currentEvent.ceventId +"&address=http://103.37.166.4:7777/StateGrid/ua.do",
					scriptCharset: 'utf-8', 
					success : function(param) {
						param = JSON.parse(param);
						if(param.stat == "1"){
							sessionStorage.setItem("usersCache" + events.currentEvent.ceventId,JSON.stringify(param.data));
							users.list = param.data;
							users.createUserList(param.data,id);
						}
					},
					error: function(){
					}
				});
			}else{
				usersCache = JSON.parse(usersCache);
				users.list = usersCache;
				users.createUserList(usersCache,id);
			}
		},
		createUserList : function(userList,id){
			var str = "";
			if(id == "#userList_q"){
				users.selectedUser = "";
				for(var i=0;i<userList.length;i++){
					if(userList[i].realizeUsers.length >0){
						str += '<li><input type="checkbox" id="'+userList[i].ecdrepId+'jsid" onclick="checkSingle(this)"><span  onclick="users.selectUser(this,\''+userList[i].ecdrepId+'\',\''+id+'\')">'+userList[i].ecdrepName+'</span><ul>';
						for(var j=0;j<userList[i].realizeUsers.length;j++){
							str += '<li><input type="checkbox" id="'+userList[i].realizeUsers[j].su_id+'yhid'+userList[i].ecdrepId+'" onclick="checkSingle(this)"><a href="javascript:;">'+userList[i].realizeUsers[j].su_name+'</a></li>';
						}
						str += '</ul></li>';
					}
				}
			}else if(id == "#userList_message"){
				users.selectedUser = "";
				str  = '';
				for(var i=0;i<userList.length;i++){
					if(userList[i].realizeUsers.length >0){
						str += '<li  onclick="users.selectUser(this,\''+i+'\')"><span>'+userList[i].ecdrepName+'</span><ul>';
						for(var j=0;j<userList[i].realizeUsers.length;j++){
							str += '<li><a href="javascript:;">'+userList[i].realizeUsers[j].su_name+'</a></li>';
						}
						str += '</ul></li>';
					}
				}
			}else{
				users.selectedUser = "ALL";
				str  = '<li class="current"  onclick="users.selectUser(this,\'ALL\')" id="allUsers"><span>所有人</span><ul></ul></li>';
				for(var i=0;i<userList.length;i++){
					if(userList[i].realizeUsers.length >0){
						str += '<li  onclick="users.selectUser(this,\''+i+'\')"><span>'+userList[i].ecdrepName+'</span><ul>';
						for(var j=0;j<userList[i].realizeUsers.length;j++){
							str += '<li><a href="javascript:;">'+userList[i].realizeUsers[j].su_name+'</a></li>';
						}
						str += '</ul></li>';
					}
				}
			}
			$(id).html(str);
			$(".current").css("background","#fff9e8");
		},
		selectUser : function(u,i,id){
			$(".current").css("background","");
			if(id == "#userList_q"){
				$(u).parent().toggleClass('current').siblings().removeClass('current');
			}else{
				$(u).toggleClass('current').siblings().removeClass('current');
			}
			if($(u).hasClass('current')){
				users.selectedUser = users.list[i];
			}else{
				$("#allUsers").addClass('current').siblings().removeClass('current');
				users.selectedUser = "ALL";
			}
			console.info(users.selectedUser);
			$(".current").css("background","#fff9e8");
		},
		selectedUser : "",
		checkedUser : "",
		list : ""
}

var questions = {
		getQuestions : function(){
			if(events.openEventsTips()){
				$.ajax({
					type : "GET",
					url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=getquestions,crid=" + events.currentEvent.ownerClassRoomId +",eventid=" + events.currentEvent.ceventId +"&address=http://103.37.166.4:7777/StateGrid/crqu.do",
					scriptCharset: 'utf-8', 
					success : function(param) {
						param = JSON.parse(param).data;
						questions.list = param
						questions.createQuestionsList(param);
					},
					error: function(){
					}
				});
			}
		},
		createQuestionsList :function (questions){
			$("#questions").html("");
			var str = "<tr><th>序号</th><th>讨论题</th><th>适用范围</th><th>参考结果</th>	<th>出题人</th><th>发布操作</th></tr>";
			var j = 0;
			for(var i in questions){
				j++;
				str += '<tr><td>'+j+'</td><td title="'+questions[i].questionText+'">'+questions[i].questionText.substring(0,10)+'</td><td title="'+questions[i].questionRange+'">'+questions[i].questionRange.substring(0,10)+'</td><td title="'+questions[i].questionAnswer+'">'+questions[i].questionAnswer.substring(0,10)+'</td><td>'+questions[i].ownerSuId+'</td><td><span class="operation"  id="fabu" onclick="questions.sendQuestions(\''+i+'\')"></span></td></tr>';
			}
			$(str).appendTo("#questions");
			$("#fabu").click(function() {
				
			});
			$(".fabuPop .close").click(function() {
				$(".fabuPop").hide(500);
				$(this).removeClass("overHiden");
			});
		},
		addQuestions : function(){
			var text = $("#question_text").val();
			var range = $("#question_range").val();
			var answer = $("#question_answer").val();
			var crid = events.currentEvent.ownerClassRoomId;
			var eventid = events.currentEvent.ceventId;
			var suid = JSON.parse(sessionStorage.getItem("suid")).data.su_id;
			if(isNull(text)&&isNull(range)&&isNull(answer)&&isNull(crid)&&isNull(eventid)&&isNull(suid)){
				$.ajax({
					type : "GET",
					url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=create,text=" + text +",range=" + range +",answer=" + answer +",crid=" + crid +",eventid=" + eventid +",suid=" + suid +"&address=http://103.37.166.4:7777/StateGrid/crqu.do",
					scriptCharset: 'utf-8', 
					success : function(param) {
						console.info(param);
						$(".discussPop").hide(500);
						$(this).removeClass("overHiden");
						questions.getQuestions();
					},
					error: function(){
					}
				});
			}else{
				$("#message2").html("请填写完整！");
				$(".pop02").show();
				$("body").addClass("overHiden");
			}
		},
		sendQuestions : function(i){
//			console.info(questions.list[i]);
			$(".fabuPop").show(500);
			$(this).addClass("overHiden");
			users.checkedUser = "";
			questions.queid = questions.list[i].questionId;
			$("#q_questionRange").val(questions.list[i].questionRange);
			$("#q_ownerSuId").val(questions.list[i].ownerSuId);
			$("#q_questionText").val(questions.list[i].questionText);
			$("#q_questionAnswer").val(questions.list[i].questionAnswer);
			$("#q_queid").val(questions.list[i].questionId);
			users.getUsers("#userList_q");
		},
		send : function(){
			var crid = events.currentEvent.ownerClassRoomId;
			var eventid = events.currentEvent.ceventId;
			var queid = questions.queid;
			var param_str = "";
			var anspid = "";
			var anssuid = "";
			if(users.checkedUser.indexOf("jsid") > -1){
				anspid = users.checkedUser.replace("jsid","");
				param_str += ",anspid=" + anspid ;
			}else if(users.checkedUser.indexOf("yhid") > -1){
//				anssuid = users.checkedUser.replace("yhid","");
				idarr = users.checkedUser.split("yhid");
				param_str += ",anspid=" + idarr[1] + ",anssuid=" + idarr[0] ;
			}
			$.ajax({
				type : "GET",
				url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=ask,crid=" + crid +",queid=" + queid +",eventid=" + eventid + param_str + "&address=http://103.37.166.4:7777/StateGrid/crqu.do",
				scriptCharset: 'utf-8', 
				success : function(param) {
					console.info(param);
					param = JSON.parse(param);
					if(param.stat == "1"){
						questions.getAnswer();
						$(".fabuPop").hide(500);
						$(this).removeClass("overHiden");
						$("#message2").html("发送成功！");
						$(".pop02").show();
						$("body").addClass("overHiden");
					}
				},
				error: function(){
				}
				
			});
		},
		getAnswer : function(){
			var crid = events.currentEvent.ownerClassRoomId;
			var eventid = events.currentEvent.ceventId;
			$.ajax({
				type : "GET",
				url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=getqaans,crid=" + crid +",eventid=" + eventid +"&address=http://103.37.166.4:7777/StateGrid/crqu.do",
				scriptCharset: 'utf-8', 
				success : function(param) {
					console.info(param);
					param = JSON.parse(param);
					if(param.stat == "1"){
						questions.createAnswer(param.data);
						User.userList(eventid);
						
					}
				},
				error: function(){
				}
				
			});
		},
		createAnswer : function(answers){
			$("#answer_q").html("");
			var str = "<tr><th>序号</th><th>讨论题</th><th>回答对象</th><th>讨论结果</th><th>提交时间</th></tr>";
			j = 0;
			var unAnswerNum = 0;
			for(var i in answers){
				j++;
				if(answers[i].eventQuestionInfo != null){
					str += '<tr><td>'+j+'</td><td title="'+answers[i].eventQuestionInfo.questionText+'">'+answers[i].eventQuestionInfo.questionText.substring(0,10)+'</td><td>'+answers[i].answerPostName+'</td><td title="'+answers[i].questionLogAnswerContent+'">'+answers[i].questionLogAnswerContent.substring(0,10)+'</td><td>'+answers[i].questionLogAnswerTime+'</td></tr>';
				}
				if(answers[i].questionLogAnswerContent.replace(/(^\s*)|(\s*$)/g,"") == ""){
					unAnswerNum++;
				}
			}
			$(str).appendTo("#answer_q");
			$("#un_answer").html("未讨论题数量："+unAnswerNum+"&nbsp");
		},
		list:{},
		queid : "",
		refresh : function(){
			if(questions.refreshState == "1"){
				questions.getQuestions();
			}else if(questions.refreshState == "2"){
				questions.getAnswer();
			}else if(questions.refreshState == "5"){
				questions.getShceme();
			}
		},
		getShceme : function(){
			var crid = events.currentEvent.ownerClassRoomId;
			var eventid = events.currentEvent.ceventId;
			if(events.openEventsTips()){
				$.ajax({
					type : "GET",
					url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=getaction,eventid=" + eventid +",crid=" + crid +"&address=http://103.37.166.4:7777/StateGrid/fact.do",
					scriptCharset: 'utf-8', 
					success : function(param) {
//						alert(param);
						param = JSON.parse(param);
						questions.shcemeList(param.data);
					},
					error: function(){
					}
				});
			}
		},
		shcemeList : function(param){
			var  user = "";
			user =JSON.parse( sessionStorage.getItem("user"))
			if(user.data == null){
				user = user.data = {};
			}else {
				user = user.data;
			}
			$("#shceme").html("");
			var user_id="";
			var str = "";
			var j = 0;
			str = '<tr><th>序号</th><th>工作组</th><th>策划者</th><th>任务内容</th><th>行动要点</th><th>执行时间</th></tr>';
			for(var i in param){
				for(var n in user){
					user_id = user[n].realizeUsers[0].su_id;
					if(user_id == param[i].suId){
						j++;
						if(param[i].relatedCases == null){
							param[i].relatedCases = {};
							param[i].relatedCases.caseContent = "";
							str += '<tr><td>'+ j +'</td><td>'+ user[n].ecdrepName +'</td><td>'+ user[n].realizeUsers[0].su_name +'</td><td title="'+param[i].relatedCases.caseContent+'">'+ param[i].relatedCases.caseContent.substring(0,30) +'</td><td title="'+param[i].actionContents+'">'+ param[i].actionContents.substring(0,30) +'</td><td>'+ param[i].actionTime +'</td></tr>';
						}else
						str += '<tr><td>'+ j +'</td><td>'+ user[n].ecdrepName +'</td><td>'+ user[n].realizeUsers[0].su_name +'</td><td title="'+param[i].relatedCases.caseContent+'">'+ param[i].relatedCases.caseContent.substring(0,30) +'</td><td title="'+param[i].actionContents+'">'+ param[i].actionContents.substring(0,30) +'</td><td>'+ param[i].actionTime +'</td></tr>';
					}
			}
			}
			$("#un_shulian").html(j);
			$(str).appendTo("#shceme");
		},refreshState : "1",
}

//查用户角色
var User = {
		userList : function(eventid){
			$.ajax({
				type : "GET",
				url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=eventusers,eventid=" + eventid +"&address=http://103.37.166.4:7777/StateGrid/ua.do",
				scriptCharset: 'utf-8', 
				success : function(param) {
//					alert(param);
					sessionStorage.setItem("user", param);
				},
				error: function(){
				}
			});
			
		}
		
}



var communication = {
		openWindow : function(){
			users.getUsers("#userList_c");
			communication.getRecord();
		},
		send : function(){
			var eventid = events.currentEvent.ceventId;
			var crid = events.currentEvent.ownerClassRoomId;
			var text = $("#content_c").val();
			var filename = "";
			var receiveData;
			var sesuid = JSON.parse(sessionStorage.getItem("suid")).data.su_id;
			var sesuname = JSON.parse(sessionStorage.getItem("suid")).data.su_name;
			var sepid = "";;
			var sepname = "";
			for(var i in users.list){
				if(users.list[i].realizeUsers != null && users.list[i].realizeUsers != ""){
					for(var j in users.list[i].realizeUsers){
						if(users.list[i].realizeUsers[j].su_id == sesuid){
							sepid = users.list[i].ecdrepId;
							sepname = users.list[i].ecdrepName;
						}
					}
				}
			}
			if(users.selectedUser == "ALL"){
				receiveData = ",resuid=allofus,resuname=allofus,repid=allofus,repname=allofus";
			}else{
				var resuid = users.selectedUser.realizeUsers[0].su_id;
				var resuname = users.selectedUser.realizeUsers[0].su_name;
				var repid = users.selectedUser.ecdrepId;
				var repname = users.selectedUser.ecdrepName;
//				var ALL ="allofus"
				receiveData = ",resuid=" + resuid +",resuname=" + resuname +",repid=" + repid +",repname=" + repname;
			}
			$.ajax({
				type : "GET",
				url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=insert,crid=" + crid +",eventid=" + eventid +",text=" + text +",filename=" + filename +",sesuid=" + sesuid +",sesuname=" + sesuname +",sepid=" + sepid +",sepname=" + sepname + receiveData +"&address=http://103.37.166.4:7777/StateGrid/cha.do",
				scriptCharset: 'utf-8', 
				success : function(param) {
					console.info(param);
					param = JSON.parse(param);
					if(param.stat == "1"){
						$("#content_c").val("");
						if(param.data.recvUserName == "allofus"){
							param.data.recvUserName = "所有人";
						}
						var str = '<p class="red">我对'+param.data.recvUserName+'说：<span  style="color: #333">'+param.data.content+'</span></p>';
						$(str).appendTo("#messAll");
					}
				},
				error: function(){
				}
			});
		},
		getRecord : function(){
			var suid = JSON.parse(sessionStorage.getItem("suid")).data.su_id;
			var eventid = events.currentEvent.ceventId;
			var crid = events.currentEvent.ownerClassRoomId;
			$.ajax({
				type : "GET",
				url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=getuchat,crid=" + crid +",eventid=" + eventid +",suid=" + suid +"&address=http://103.37.166.4:7777/StateGrid/cha.do",
				scriptCharset: 'utf-8', 
				success : function(param) {
					console.info(param);
					param = JSON.parse(param);
					if(param.stat == "1"){
						console.info(param);
						//append聊天记录
						communication.createRecord(param.data);
					}
				},
				error: function(){
				}
			});
		},
		createRecord : function(record){
			var suid = JSON.parse(sessionStorage.getItem("suid")).data.su_id;
			var str = ""
			for(var i in record){
				if(record[i].sendSuId == suid){
					str += '<p class="red">';
					record[i].sendUserName = "我";
				}else{
					str += '<p class="">';
				}
				if(record[i].recvUserName == "allofus" ){
					record[i].recvUserName = "所有人";
				}
				if(record[i].recvSuId == suid ){
					record[i].recvUserName = "我";
				}
				str += record[i].sendUserName + '对'+ record[i].recvUserName +'说：<span style="color: #333">'+ record[i].content +'</span></p>';
			}
			$("#messAll").html(str);
		}
}

var time_num = 0;
var timer = 0;
$(function() {
	$("#cName").html(sessionStorage.getItem("ecdClassRoomName"));
	events.getEventsList();
	$("#eventFlow").addClass("active");
	$(".eventFlow").css("visibility", "visible");
	$("#condition_str").keyup(function(){
		messages.searchMessage();
	 });
	$("#clearAll").click(function(){
		$("#condition_str").val("");
		messages.searchMessage();
	});
	$("#temporaryAdd").click(function() {
		users.selectedUser = "";
		$("#upladName").val("")
		$("#sendorder").val("");
		$("#sendtypeid").val("");
		$("#text").val("");
		$("#sender").val("");
		$("#eventname").html(events.currentEvent.ceventName);
		console.info(new Date().Format("yyyy-MM-ddTHH:mm:ss"));
		$("#sendtime").val(new Date().Format("yyyy-MM-ddTHH:mm:ss"));
		users.selectedUser = "";
		users.getUsers("#userList_message");
	});
	$("#addMessage").click(function(){
		messages.addMessage(this);
	});
	$("#responsive").click(function() {
		if($(this).hasClass('active')) {
			questions.getQuestions();
			questions.getAnswer();
			questions.getShceme();
		}
	});
	$("#question_save").click(function(){
		questions.addQuestions();
	});
	$("#addDiscuss").click(function() {
		users.selectedUser = "";
		$("#question_text").val("");
		$("#question_range").val("");
		$("#question_answer").val("");
	});
	$("#message").click(function() {
		if($(this).hasClass('active')) {
			communication.openWindow();
		}
	});
	$("#send_q").click(function(){
		questions.send();
	});
	$("#tab1").click(function(){
		questions.refreshState = "1";
	});
	$("#tab2").click(function(){
		questions.refreshState = "2";
	});
	$("#tab3").click(function(){
		questions.refreshState = "3";
	});
	$("#tab4").click(function(){
		questions.refreshState = "4";
	});
	$("#tab5").click(function(){
		questions.refreshState = "5";
	});
	$("#tab6").click(function(){
		questions.refreshState = "6";
	});
	$("#tab7").click(function(){
		questions.refreshState = "7";
	});
	$("#sned_c").click(function(){
		communication.send();
	});
	
//	alert(new Date("2010/04/20 08:03:56"));
});

function checkSingle(u){
	if($(u).is(':checked')){
		$(':checkbox[type="checkbox"]').removeAttr('checked');
		$(u).prop('checked','checked');
//		alert();
		users.checkedUser = u.id;
	}else{
		users.checkedUser = "";
	}
}
function createTime(t){
	timer = setInterval(function(){
		time_num++;
//	console.info(current_date("2010/04/20 08:03:56"));
		var time = current_date(t);
		$("#current_time").html("当前："+time);
	},1000);
}
function current_date(date){
	date = new Date(date);
	date = date.getTime();
	date = new Date(date + time_num*1000);
	return date.Format("yyyy/MM/dd HH:mm:ss");
}
//$("#message1").html(runFunction.messages);
var runFunction = {
		show : function(){
			$(".pop02").show();
			$("body").addClass("overHiden");
		},
		close : function(){
			$(".pop02").hide();
			$("body").removeClass("overHiden");
		}
}
Date.prototype.Format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "H+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}


